<template>
  <TCollapse title="项目验收信息">
    <el-form :model="accTimeInfo" :rules="rules" label-width="160px" class="pms-form" size="mini" :inline="true">
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item v-if="pageList.includes('resourceIntime')" label="资源录入时间" style="width: 100%" prop="resourceInTime">
            <el-date-picker v-model="accTimeInfo.resourceInTime" :disabled="true" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item v-if="pageList.includes('resourceConfirmtime')" label="资源确认时间" style="width: 100%" prop="resourceConfirmTime">
            <el-date-picker v-model="accTimeInfo.resourceConfirmTime" :disabled="true" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item v-if="pageList.includes('firstAcceptTime')" label="初验时间" style="width: 100%" prop="firstAcceptTime">
            <el-date-picker
              v-model="accTimeInfo.firstAcceptTime"
              :clearable="false"
              :disabled="!['F91','F92'].includes(prjDetail.btnType) || +type===1"
              :picker-options="{disabledDate:isDisFirstAcceptTime}"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item v-if="pageList.includes('finalTime')" label="终验时间" style="width: 100%" prop="endInternalAcceptDate">
            <el-date-picker
              v-model="accTimeInfo.endInternalAcceptDate"
              :clearable="false"
              :disabled="!['F93','F94'].includes(prjDetail.btnType) || +type===1"
              :picker-options="{disabledDate:isDisEndInternalAcceptDate}"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item v-if="pageList.includes('accTime')" label="验收时间" style="width: 100%" prop="endInternalAcceptDate">
            <el-date-picker
              v-model="accTimeInfo.endInternalAcceptDate"
              :clearable="false"
              :disabled="+type===1"
              :picker-options="{disabledDate:isDisFirstAcceptTime}"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <!--    自主研发不展示按钮-->
    <YSCommonButton v-if="![12].includes(prjDetail.pageType) && type==='todo'" :prj-id="prjDetail.prjId" :button-type="accButtonType" />
  </TCollapse>
</template>

<script>
import YSCommonButton from './YSCommonButton'
import TCollapse from '@/components/basic/TCollapse.vue'
import { getAcceptProcessInitiationTime, getAccTimeInfo } from '@/api/acceptanceManagement/acceptanceMange'
import { getQueryObject } from '@/utils'
const options = {
  15: [],	// 科技创新
  13: ['firstAcceptTime', 'finalTime'],	// 购置
  21: [],	// 租赁费用
  23: [],	// 维保费用
  11: ['firstAcceptTime', 'finalTime'],	// 套装软件
  12: ['accTime'],	// 自主研发
  14: ['resourceIntime', 'resourceConfirmtime', 'firstAcceptTime', 'finalTime'],	// /工程建设
  22: []	// 外包费用
}

export default {
  name: 'PrjAcceptInfo',
  components: { TCollapse, YSCommonButton },
  props: {
    prjDetail: {
      type: Object,
      required: true,
      default: () => ({})
    }
  },
  data() {
    return {
      type: '',
      accTimeInfo: {
        resourceInTime: '',
        resourceConfirmTime: '',
        endInternalAcceptDate: '',
        firstAcceptTime: '',
        accProInitiationTime: ''
      },
      rules: {
        resourceInTime: [
          { required: true, message: '请选择资源录入时间', trigger: 'blur' }
        ],
        resourceConfirmTime: [
          { required: true, message: '请选择资源确认时间', trigger: 'blur' }
        ],
        firstAcceptTime: [
          { required: true, message: '请选择验收时间', trigger: 'blur' }
        ],
        endInternalAcceptDate: [
          { required: true, message: '请选择验收时间', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    pageList() {
      return options[this.prjDetail.pageType] || []
    },
    accButtonType() {
      /**
       * 各个阶段对应的编码
       * F4  资源录入
       * F5  资源确认
       * F10 验收申请
       * F11 验收审核
       * F91 初验申请
       * F92 初验审核
       * F93 终验申请
       * F94 终验审核
       */
      /**
       * 按钮展示控制
       * entering 资源录入
       * audit  资源确认
       * look   资源查看
       */
      /**
       * accType 跳过前面节点
       * 1: 初验加终验
       * 2: 仅终验
       */
      // 工程 除了资源录入其他都只能查看
      if (this.prjDetail.pageType === 14) {
        if (!['F4', 'F5'].includes(this.prjDetail.btnType)) {
          return 'look'
        }
      }
      //  购置和套装 如果只有终验,那么初验的录入按钮放到终验上
      if ([11, 13].includes(this.prjDetail.pageType) && this.prjDetail.accType === 2) {
        if (this.prjDetail.btnType === 'F93') {
          return 'entering'
        }
        if (this.prjDetail.btnType === 'F94') {
          return 'audit'
        }
      }

      if (['F4', 'F91'].includes(this.prjDetail.btnType)) {
        return 'entering'
      }
      if (['F5', 'F92'].includes(this.prjDetail.btnType)) {
        return 'audit'
      }
      if (['F3', 'F93', 'F94'].includes(this.prjDetail.btnType)) {
        return 'look'
      }
      return 'look'
    },
    isNowDay() {
      const now = new Date(this.accTimeInfo.accProInitiationTime)
      return new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0).getTime()
    },
    resourceConfirmTime() {
      const now = new Date(this.accTimeInfo.resourceConfirmTime)
      return new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0).getTime()
    },
    firstAcceptTime() {
      const now = new Date(this.accTimeInfo.firstAcceptTime)
      return new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0).getTime()
    }
  },
  created() {
    // 获取时间信息
    const { type } = getQueryObject()
    this.type = type
    this.getAccTime()
  },
  methods: {
    // 校验时间是否填写
    submit() {
      // 初验
      if (['F10', 'F11'].includes(this.prjDetail.btnType)) {
        if (this.accTimeInfo.endInternalAcceptDate) {
          return {
            success: true,
            data: {
              prjId: this.prjDetail.prjId,
              endInternalAcceptDate: this.accTimeInfo.endInternalAcceptDate
            }
          }
        } else {
          return {
            success: false,
            message: '请选择验收时间'
          }
        }
      }
      // 初验
      if (['F91', 'F92'].includes(this.prjDetail.btnType)) {
        if (this.accTimeInfo.firstAcceptTime) {
          return {
            success: true,
            data: {
              prjId: this.prjDetail.prjId,
              firstAcceptTime: this.accTimeInfo.firstAcceptTime
            }
          }
        } else {
          return {
            success: false,
            message: '请选择初验时间'
          }
        }
      }
      // 终验时间
      if (['F93', 'F94'].includes(this.prjDetail.btnType)) {
        if (this.accTimeInfo.endInternalAcceptDate) {
          const endInternalAcceptDate = new Date(this.accTimeInfo.endInternalAcceptDate).getTime()
          if (this.firstAcceptTime > endInternalAcceptDate) {
            return {
              success: false,
              message: '终验时间不能小于初验时间'
            }
          }
          return {
            success: true,
            data: {
              prjId: this.prjDetail.prjId,
              endInternalAcceptDate: this.accTimeInfo.endInternalAcceptDate
            }
          }
        } else {
          return {
            success: false,
            message: '请选择终验时间'
          }
        }
      }
      return {
        success: true,
        data: {
          prjId: this.prjDetail.prjId
        }
      }
    },
    // 初验时间限制(当前时间到最后节点审核之间)
    // 工程建设,小于资源确认时间
    isDisFirstAcceptTime(time) {
      if (+this.prjDetail.pageType === 14) {
        return time.getTime() > Date.now() || time.getTime() < this.resourceConfirmTime
      } else {
        return time.getTime() > Date.now() || time.getTime() < this.isNowDay
      }
    },
    // 终验时间限制(当前时间到初验时间之间)
    isDisEndInternalAcceptDate(time) {
      if (+this.prjDetail.accType === 2) {
        return time.getTime() > Date.now() || time.getTime() < this.isNowDay
      } else {
        return time.getTime() > Date.now() || time.getTime() < this.firstAcceptTime
      }
    },
    // 获取验收相关时间
    getAccTime() {
      getAccTimeInfo({ prjId: this.prjDetail.prjId }).then(res => {
        if (res.code === '200') {
          for (const i in res.data) {
            this.$set(this.accTimeInfo, i, res.data[i])
          }
        }
      })
      // 获取最后节点时间
      getAcceptProcessInitiationTime({ prjId: this.prjDetail.prjId }).then(res => {
        if (res.code === '200') {
          this.accTimeInfo.accProInitiationTime = res.data || new Date()
        }
      })
    }

  }
}
</script>
